package servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class LogFilter implements Filter {
	
	private FilterConfig config;

	@Override
	public void destroy() {
		this.config=null;
		System.out.println("-------filter-----destroy-------");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		System.out.println("-------filter-----doFilter-------");
		
		//----------下面代码对用户请求进行预处理--------------------
		ServletContext context=this.config.getServletContext();
		long before=System.currentTimeMillis();
		System.out.println("--开始过滤。。。");
		//将请求转成HttpServletRequest请求
		HttpServletRequest hrequest=(HttpServletRequest)request;
		//输出提示信息
		System.out.println("Filter已经截取到用户的请求地址："+
				hrequest.getServletPath());
		
		
		//＊＊＊＊＊ Filter只是链式处理，请求依然放行到目的地址
		filterChain.doFilter(request, response);
		
		
		//-------------下面代码用于对服务器响应执行后处理---------------
		long after=System.currentTimeMillis();
		//输出提示信息
		System.out.println("--过滤结束");
		System.out.println("请求被定位到"+hrequest.getRequestURI()+" "
				+ "所花时间为 "+(after-before));
		
		
		
		
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config=config;
		System.out.println("-------filter-----init-------");
	}

}
